#-*- codeing = utf-8 -*-
#@Time : 2020/9/13 13:06
#@Author : 阳某
#@File : 多任务异步1.py
#@Software : PyCharm

# 使用该模块中的clientsession对象
import aiohttp
import asyncio
import time
start = time.time()
urls = [
    'http://127.0.0.1:5000/aa',
    'http://127.0.0.1:5000/bb',
    'http://127.0.0.1:5000/cc',
]
async def get_page(url):
    async with aiohttp.ClientSession() as session:
        # get(),post()
        # headers,params,data,ua,cookie,proxy='http://ip:port'
        async with await session.get(url)as response:        #有耗时阻塞操作，必须手动挂起（await）
            # response.text()  字符串
            # response.read() 二进制
            # response.json()json对象
            # 注意：获取相应数据操作之前一定要进行挂起
            page_text =await response.text()
            print(page_text)
tasks = []
for url in urls:
    c = get_page(url)
    task = asyncio.ensure_future(c)
    tasks.append(task)

loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
end = time.time()
print('总耗时:',end-start)     #多任务异步协程2s